﻿using System;
using System.Reflection;
using System.Web.UI.WebControls;
using System.Xml;
using Medianamik.Core;
using Medianamik.Globalization;
using Telerik.Web.UI;
using System.Linq;

namespace Medianamik.UI.Controls
{
    /// <summary>
    /// This is the same as the current Editor with only base fonction like :
    /// - Bold
    /// - Italic
    /// - Link
    /// - Ordered list (UL / LI)
    /// - Undo / Redo
    /// </summary>
    public class LiteEditor : BaseEditor
    {
        protected override void CreateControlHierarchy(bool pUseDataSource)
        {
            #region Editor

            Controls.Add(editor);
            editor.ID = ControlToValidateId;
            editor.Skin = Skins.DEFAULT_SKIN;
            editor.EnableEmbeddedSkins = Skins.DEFAULT_USE_EMBEDDED_SKIN;

            if (pUseDataSource)
            {
                editor.Content = (DataSource == null) ? String.Empty : DataSource.ToString();
                editor.CssClass = "Editor";

                editor.Width = ControlInfo.Width.HasValue 
                             ? ControlInfo.Width.Value 
                             : WIDTH;

                editor.Height = ControlInfo.Height.HasValue 
                              ? ControlInfo.Height.Value 
                              : HEIGHT;

                string editorXml = GetEditorXml();
                var editorXmlDoc = new XmlDocument();
                editorXmlDoc.LoadXml(editorXml);
                editor.LoadToolsFile(editorXmlDoc);

                editor.EditModes = EditModes.Design ^ EditModes.Html;
                SetEditorToolbarMode(editor);

                //Voir Benoit Renaud
                editor.NewLineBr = false;

                editor.ContentFilters = EditorFilters.DefaultFilters;
                editor.ContentFilters &= ~EditorFilters.FixEnclosingP;
                editor.ContentFilters &= ~EditorFilters.IECleanAnchors;
                editor.ContentFilters &= ~EditorFilters.MakeUrlsAbsolute;

                editor.OnClientPasteHtml = "OnClientPasteHtml";

                editor.CssFiles.Clear();

                if (CurrentSite == null || string.IsNullOrEmpty(CurrentSite.CSSFilePath))
                {
                    editor.CssFiles.Add(Config.DefaultEditorCSSFilePath);
                }
                else
                {
                    editor.CssFiles.Add(CurrentSite.CSSFilePath);
                }

                ConfigureDialogs();
                editor.DialogsCssFile = "/Medianamik/includes/medianamik.css?v=" +
                    Utility.GetFileVersion();

                //Provide an option for clearing the style
                AddParagraphs("Controls.Editor.Paragraphs.xml");
            }
            
            #endregion
        }

        protected override string GetEditorXml()
        {
            return Core.Sugar.Resources.GetResourceText("Controls.Editor.EditorLite.xml", 
                                                        Assembly.GetExecutingAssembly());
        }

        protected internal void ConfigureDialogs()
        {
            SetDialogPaths(editor.DocumentManager, Config.EditorDocumentFilesPath);
            SetDialogSearchPatterns(editor.DocumentManager, Config.EditorDocumentFilesExtension, "*.doc", "*.txt", "*.docx", "*.xls", "*.xlsx", "*.pdf");
        }
    }
}
